package cn.iocoder.yudao.module.vos.controller.admin.gatewaylevel;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.vos.controller.admin.gatewaylevel.vo.*;
import cn.iocoder.yudao.module.vos.dal.dataobject.gatewaylevel.GatewayLevelDO;
import cn.iocoder.yudao.module.vos.service.gatewaylevel.GatewayLevelService;

@Tag(name = "管理后台 - 网关等级")
@RestController
@RequestMapping("/vos/gateway-level")
@Validated
public class GatewayLevelController {

    @Resource
    private GatewayLevelService gatewayLevelService;

    @PostMapping("/create")
    @Operation(summary = "创建网关等级")
    @PreAuthorize("@ss.hasPermission('vos:gateway-level:create')")
    public CommonResult<Long> createGatewayLevel(@Valid @RequestBody GatewayLevelSaveReqVO createReqVO) {
        return success(gatewayLevelService.createGatewayLevel(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新网关等级")
    @PreAuthorize("@ss.hasPermission('vos:gateway-level:update')")
    public CommonResult<Boolean> updateGatewayLevel(@Valid @RequestBody GatewayLevelSaveReqVO updateReqVO) {
        gatewayLevelService.updateGatewayLevel(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除网关等级")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('vos:gateway-level:delete')")
    public CommonResult<Boolean> deleteGatewayLevel(@RequestParam("id") Long id) {
        gatewayLevelService.deleteGatewayLevel(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除网关等级")
                @PreAuthorize("@ss.hasPermission('vos:gateway-level:delete')")
    public CommonResult<Boolean> deleteGatewayLevelList(@RequestParam("ids") List<Long> ids) {
        gatewayLevelService.deleteGatewayLevelListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得网关等级")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('vos:gateway-level:query')")
    public CommonResult<GatewayLevelRespVO> getGatewayLevel(@RequestParam("id") Long id) {
        GatewayLevelDO gatewayLevel = gatewayLevelService.getGatewayLevel(id);
        return success(BeanUtils.toBean(gatewayLevel, GatewayLevelRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得网关等级分页")
    @PreAuthorize("@ss.hasPermission('vos:gateway-level:query')")
    public CommonResult<PageResult<GatewayLevelRespVO>> getGatewayLevelPage(@Valid GatewayLevelPageReqVO pageReqVO) {
        PageResult<GatewayLevelDO> pageResult = gatewayLevelService.getGatewayLevelPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, GatewayLevelRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出网关等级 Excel")
    @PreAuthorize("@ss.hasPermission('vos:gateway-level:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportGatewayLevelExcel(@Valid GatewayLevelPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<GatewayLevelDO> list = gatewayLevelService.getGatewayLevelPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "网关等级.xls", "数据", GatewayLevelRespVO.class,
                        BeanUtils.toBean(list, GatewayLevelRespVO.class));
    }

}